package com.icedevcloud.upms.controller.admin.permission;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.icedevcloud.common.core.api.R;
import com.icedevcloud.common.core.log.IceLog;
import com.icedevcloud.common.security.util.SecurityUtil;
import com.icedevcloud.upms.controller.admin.permission.dto.AddPermissionReqDto;
import com.icedevcloud.upms.controller.admin.permission.dto.PermissionQueryReqDto;
import com.icedevcloud.upms.controller.admin.permission.dto.RouterResDto;
import com.icedevcloud.upms.controller.admin.permission.dto.UpdatePermissionReqDto;
import com.icedevcloud.upms.entity.SysPermission;
import com.icedevcloud.upms.service.ISysPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2023-08-13
 */
@Tag(name = "权限接口类")
@RequiredArgsConstructor
@RestController
@RequestMapping("/sysPermission")
public class SysPermissionController {

    private final ISysPermissionService iSysPermissionService;

    @IceLog
    @Operation(summary = "路由接口")
    @GetMapping("/getRouters")
    public R<List<RouterResDto>> getRouters() {
        List<RouterResDto> result = iSysPermissionService.getRouters();
        return R.ok(result);
    }

    @IceLog
    @Operation(summary = "获取权限编码接口")
    @GetMapping("/getPermCode")
    public R<List<String>> getPermCode() {
        Long userId = SecurityUtil.getCurrentLoginUserId();
        List<String> permCode = iSysPermissionService.getUserPermCodeByUserId(userId);
        return R.ok(permCode);
    }

    @IceLog
    @SaCheckPermission("upms:permission:page")
    @Operation(summary = "权限列表接口")
    @PostMapping("/permissionList")
    public R<List<SysPermission>> permissionList(@RequestBody PermissionQueryReqDto reqDto) {
        List<SysPermission> result = iSysPermissionService.permissionList(reqDto);
        return R.ok(result);
    }

    @IceLog
    @SaCheckPermission("upms:permission:add")
    @Operation(summary = "添加权限接口")
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody @Validated AddPermissionReqDto reqDto) {
        Boolean result = iSysPermissionService.add(reqDto);
        return R.ok(result);
    }

    @IceLog
    @SaCheckPermission("upms:permission:update")
    @Operation(summary = "更新权限接口")
    @PutMapping("/update")
    public R<Boolean> update(@RequestBody @Validated UpdatePermissionReqDto reqDto) {
        Boolean result = iSysPermissionService.update(reqDto);
        return R.ok(result);
    }

    @IceLog
    @SaCheckPermission("upms:permission:delete")
    @Operation(summary = "删除权限接口")
    @DeleteMapping("/delete")
    public R<Boolean> delete(@NotNull(message = "ID不能为空") Long id) {
        Boolean result = iSysPermissionService.delete(id);
        return R.ok(result);
    }

}
